<?php

global $cv_option_data;
// Grab all the options data
function cv_get_option() {
	global $cv_option_data;
	if ( !isset( $cv_option_data ) )
		$cv_option_data = get_option(OPTIONS);
	
	if ( !is_array( $cv_option_data ) || empty( $cv_option_data ) )
		return false;		

	return $cv_option_data;
}

function cv_categories() {
		//Access the WordPress Categories via an Array
		$of_categories = array();  
		$of_categories_obj = get_categories('hide_empty=0');
		foreach ($of_categories_obj as $of_cat) {
			$of_categories[$of_cat->cat_ID] = $of_cat->cat_name;}
		$categories_tmp = array_unshift($of_categories, "Select a category:");
		
		return $of_categories;
	}	

function cv_pages() {	
		//Access the WordPress Pages via an Array
		$of_pages = array();
		$of_pages_obj = get_pages('sort_column=post_parent,menu_order');    
		foreach ($of_pages_obj as $of_page) {
			$of_pages[$of_page->ID] = $of_page->post_name; }
		$of_pages_tmp = array_unshift($of_pages, "Select a page:"); 
		
		return $of_pages;
	}	

//Stylesheets Reader
function cv_styles($alt_stylesheet_path) {
		$alt_stylesheets = array();
		
		if ( is_dir($alt_stylesheet_path) ) {
			if ($alt_stylesheet_dir = opendir($alt_stylesheet_path) ) { 
				while ( ($alt_stylesheet_file = readdir($alt_stylesheet_dir)) !== false ) {					
					if(stristr($alt_stylesheet_file, ".css") !== false) {
						$alt_stylesheets[] = $alt_stylesheet_file;
					}
				}    
			}
		}		
		return $alt_stylesheets;
	}
	
/*-----------------------------------------------------------------------------------*/
/* Get page ID */
/*-----------------------------------------------------------------------------------*/
function get_page_id($page_slug){
	$page_id = get_page_by_path($page_slug);
    if ($page_id) {
        return $page_id->ID;
    } else {
        return null;
    }

}
/*-----------------------------------------------------------------------------------*/
/* Get page ID */
/*-----------------------------------------------------------------------------------*/
function get_category_id($category_name){
	foreach(get_categories('hide_empty=0') as $cat){ //loop through categories
    	if($cat->name==$category_name)
			return $cat->cat_ID;
    }  
    return null; 
}

// only used in sorter
function get_categories_id($category_names){
	$catIDs = '';
	$i = 0;	
	foreach($category_names as $key=>$catname){ 
		if($key == "placebo") continue;
		$i++;
		if($i == 1)
			$catIDs = $key;
		else
			$catIDs .= ',' . $key;
	}
				
    return $catIDs; 
}

function of_font_stack($font){
	$stack = '';
	
	switch ( $font ) {
	
		case 'arial':
			$stack .= 'Arial, sans-serif';
		break;
		case 'verdana':
			$stack .= 'Verdana, "Verdana Ref", sans-serif';
		break;
		case 'trebuchet':
			$stack .= '"Trebuchet MS", Verdana, "Verdana Ref", sans-serif';
		break;
		case 'georgia':
			$stack .= 'Georgia, serif';
		break;
		case 'times':
			$stack .= 'Times, "Times New Roman", serif';
		break;
		case 'tahoma':
			$stack .= 'Tahoma,Geneva,Verdana,sans-serif';
		break;
		case 'palatino':
			$stack .= '"Palatino Linotype", Palatino, Palladio, "URW Palladio L", "Book Antiqua", Baskerville, "Bookman Old Style", "Bitstream Charter", "Nimbus Roman No9 L", Garamond, "Apple Garamond", "ITC Garamond Narrow", "New Century Schoolbook", "Century Schoolbook", "Century Schoolbook L", Georgia, serif';
		break;
		case 'helvetica':
			$stack .= '"Helvetica Neue", Helvetica, Arial, sans-serif';
		break;
	}
	return $stack;
}

/*-----------------------------------------------------------------------------------*/
/* Get feature content */
/*-----------------------------------------------------------------------------------*/
function cv_feature_content($opt, $content, $class = '', $before = null, $after = null, $return_info = null, $default_img = null){
		$cv_option = cv_get_option();
		$feature_option = $cv_option[$opt];
		
		$show = false;
		if ( $feature_option['type'] == 'page' && $feature_option['page'] && $feature_option['page'] <> "Select a page:") { 
			$args = array('name' => $feature_option['page'] ,'post_type' => $feature_option['type'],'post_status' => 'publish','caller_get_posts'=> 1);
			$show = true;
		}
		elseif($feature_option['type'] == 'post' && $feature_option['post']) {
			$args = array('p' => $feature_option['post'] ,'post_type' => $feature_option['type'],'post_status' => 'publish','caller_get_posts'=> 1);
			$show = true;
		}
		
		if ($show) {				
			
			query_posts($args);
			global $post;					
			if (have_posts()) : while (have_posts()) : the_post();				
			echo '<div id="'. $feature_option['type'] .'-'. $post->ID .'" class="feature-content hentry ' . $class . ' clearfix">';
			
				if($before)	echo $before;
				
				if ( in_array( 'title', $content) ) {			
					echo apply_atomic_shortcode( 'entry_title', '[entry-title]' );
				}	
				
				if ( in_array( 'thumbnail', $content) || array_key_exists( 'thumbnail', $content ) ) {
					if(array_key_exists( 'thumbnail', $content ))
						$thumb = $content['thumbnail'];
					else
						$thumb = 'Thumbnail';
					echo '<div class="entry-img">';	
					
					if ( current_theme_supports( 'get-the-image' ) ) $thumb = get_the_image( array( 'format' => 'array', 'echo' => false, 'custom_key' => array( 'Thumbnail', 'thumbnail' ), 'default_size' => $thumb, 'default_image' => $default_img ) );
					?>
					<a href="<?php echo the_permalink(); ?>"></a><strong></strong><img src="<?php echo $thumb['src']; ?>">
					<?php
					
					echo '</div><!-- .entry-img -->';	
				}
				
				if ( in_array( 'content', $content ) ) {
					echo '<div class="entry-content">';
						 global $more;  $more=0; 
						echo the_content();	
					echo '</div> <!-- .entry-content -->';	
				}
				
				if ( in_array( 'excerpt', $content ) ) {				
					echo '<div class="entry-summary">';
						 global $more;  $more=0; 
						echo the_excerpt();	
					echo '</div> <!-- .entry-summary -->';	
				}
				
				if ( in_array( 'byline', $content ) ) 
					echo apply_atomic_shortcode( 'byline', '<div class="entry-byline">' . __( 'By [entry-author] on [entry-published] [entry-edit-link before=" | "]', hybrid_get_textdomain() ) . '</div>' );
				
				if($after)	echo $after;
				
				if($return_info=='day-month') $r = get_the_time('d;n');
				if($return_info=='link') $r = get_permalink();
				
			echo '</div><!-- .hentry -->';		
				
			endwhile; endif; 
												
			wp_reset_query();
				
			return $r;
		}
	}

?>
